var core = require('../../utils/core.js')
// 引入SDK核心类
var app=getApp()
Page({
  data: {
    focus: false,
    inputValue: '',


    imgs: [],
    urls:[],
    qcode:'',

    
    fee:0,
    

    
    label_id:0,
    label_name:'',
    lat:'',
    lng:'',
    address:'',
    addr_name:'',

    title:'',
    desc:'',
    stage:[],//项目阶段
    stageid:0,

    from:[],//项目来源
    from_id:0,

    projectType:[],//项目类型
    projectTypeId:0,

    invest_money:'',//投资金额
    unit:'',//占地面积

    
    selectArrayFlag:false,
    selectIndex:'',
    selectArray: [ 
    ],
    city:[],//意向区域
    city_id:0,
    city_str:'',


    policy:[],//配套政策
    policy_id:0,
    policy_str:'',


    house:[],//落户要求
    house_id:0,
    house_str:'',
    value:'',
    tax:'',

    

    tel:'',
    wx:'',
    fee_type:[0,50,100,150,300,500],

    more1:0,
    more2:0,
    file_type:'',
    file_size:''
  },
  onShow:function(){
   
    var label=wx.getStorageSync('label')
    
    if(label){
      label=JSON.parse(label);
      this.setData({label_id:label.id,label_name:label.name})
    }
  },
  onLoad:function(){
      this.stage_type()//项目阶段
      this.from_type()//项目来源
      this.projectType_type()//项目类型
      this.city_type()//意向区域
      this.policy_type()//政策要求
      this.house_type()//落户要求
  },
 
  ///////////////下拉多选菜单显示start////////////////////
  selectArrayFlag:function(res){
    
     var index=res.currentTarget.id
     console.log(index)
     this.setData({selectArray:this.data[index],selectIndex:index})
     this.setData({selectArrayFlag:true})
  },
  selectConfirm:function(res){
    var index=res.currentTarget.id
    this.setData({[index]:this.data.selectArray,selectArrayFlag:false,selectArray:[]})

    var data=this.data[index];
    var arr=[]
    var brr=[]
    for(var i=0;i<data.length;i++){
      if(data[i].flag==1){
        arr.push(data[i].name)
        brr.push(data[i].id)
      }
    }
    var index_str=index+'_str';
    var index_id=index+'_id';
    this.setData({[index_str]:arr.join(','),[index_id]:brr.join(',')})
  },
  selectCancle:function(res){
    var index=res.currentTarget.id
    this.setData({selectArrayFlag:false,selectArray:[]})
  },
  checkbox:function(res){
   
    var data=res.currentTarget.id.split('&&');
    var flag=data[0]*1==1?0:1;
    var item=this.data.selectArray
    item[data[1]].flag=flag
    this.setData({selectArray:item})
   
  },
///////////////////////下拉多选菜单显示end/////////////////////////////////////





////////////////////标签start///////////////////////////
  label:function(){
    wx.navigateTo({
      url:"/pages/release/label"
    })
  },
   //项目阶段
  stage_type:function(){
  var self=this
  core.post("label",{op:'stage'},function(res){
      if(res.code==1){
        self.setData({stage:res.data})
      }
  })
},
   //项目来源
 from_type:function(){
    var self=this
    core.post("label",{op:'from'},function(res){
        if(res.code==1){
          self.setData({from:res.data})
        }
    })
  },
 //项目类型
 projectType_type:function(){
  var self=this
  core.post("label",{op:'projectType'},function(res){
      if(res.code==1){
        self.setData({projectType:res.data})
      }
  })
},
   //意向区域标签
 city_type:function(){
      var self=this
      core.post("label",{op:'city'},function(res){
          if(res.code==1){
            var data=[
              {id:0,city_name:'不限区域',flag: true ,name: "不限区域"},
            ];
            res.data.forEach(function(item, index){
              data.push(item)
            })
            self.setData({city:data})
          }
      })
    },
  //政策要求标签
  policy_type:function(){
    var self=this
    core.post("label",{op:'policy'},function(res){
        if(res.code==1){
          self.setData({policy:res.data})
        }
    })
  },
  //落户要求标签
  house_type:function(){
    var self=this
    core.post("label",{op:'house'},function(res){
        if(res.code==1){
          self.setData({house:res.data})
        }
    })
  },
  ///////////////////标签end///////////////////////////
  bindPickerChange:function(res){
    var index=res.target.id
    this.setData({[index]:res.detail.value})
  },
 


   // 上传图片
   chooseImg: function (e) {
    var that = this;
    var imgs=this.data.imgs
    core.upload(function(res){
      // console.log(res)
      if(res.error==0){
        //imgs.push(res.url);
        //that.setData({imgs:imgs})
        that.setData({imgs:[res.url]})
      }
    });
  },
  // 删除图片
  deleteImg: function (e) {
    var imgs = this.data.imgs;
    var index = e.currentTarget.dataset.id;
   var that=this
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (sm) {
        if (sm.confirm) {
            // 用户点击了确定 可以调用删除方法了
            imgs.splice(index, 1);
            that.setData({
              imgs: imgs
            });
          } else if (sm.cancel) {
            //console.log('用户点击取消')
          }
        }
    })
  },
  


   // 上传二维码图片
   chooseqcode: function (e) {
    var that = this;
    core.upload(function(res){
      // console.log(res)
      if(res.error==0){
        that.setData({qcode:res.url})
      }
    });
  },
  // 删除二维码
  deleteqcode: function (e) {
   
   var that=this
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (sm) {
        if (sm.confirm) {
            // 用户点击了确定 可以调用删除方法了
            that.setData({
              qcode: ''
            });
          } else if (sm.cancel) {
            //console.log('用户点击取消')
          }
        }
    })
  },
  getMobile:function(e){
    var iv=e.detail.iv
    var self=this
    var encryptedData=e.detail.encryptedData
    wx.login({
      success: res => {
       
        core.post('Getopenid',{code:res.code},function(res){
          var  data=JSON.parse(res.content);
          
          core.post('GetUserphone',{sessionKey:data.session_key,iv:iv,encryptedData:encryptedData},function(res){
            console.log(res)
           if(res.data.phoneNumber){
              self.setData({tel:res.data.phoneNumber})        
           }
          })

        })
      }
    })
  },
  
  getval(e) {
    var name=e.target.id
    this.setData({
      [name]: e.detail.value
    })
  },
  submit:function(){

    var self=this
    //必填验证
    var flag=false;
    var msg=''
    if(!self.data.title&&flag==false){
      flag=true,
      msg="项目名称必填"
    }
    if(!self.data.label_id&&flag==false){
      flag=true,
      msg="项目分类必选"
    }
    if(!self.data.address&&flag==false){
      flag=true,
      msg="地址必填"
    }
    if(self.data.imgs.length==0&&flag==false){
      flag=true,
      msg="占位图必传"
    }
    if(self.data.urls.length==0&&flag==false){
      flag=true,
      msg="PDF必传"
    }
    
    if(!self.data.tel&&flag==false){
      flag=true,
      msg="联系方式必填"
    }
    // if(!self.data.wx&&flag==false){
    //   flag=true,
    //   msg="微信号必填"
    // }
    if(flag==true){
      wx.showToast({
        'title':msg,
        'icon':'none'
      })
      return
    }
    wx.showLoading({
      title: '耐心等待中',
    })
    var data={
      name:self.data.title,
      label_id:self.data.label_id,
      address:self.data.address,
      lat:self.data.lat,
      lng:self.data.lng,
      imgs:JSON.stringify(self.data.imgs),
      urls:JSON.stringify(self.data.urls),
      tel:self.data.tel,
      unit:self.data.unit,
      invest_money:self.data.invest_money,
      op:'add'
     }
    // console.log(data)
    // return
    core.post("Localprojects",data,function(res){
      wx.hideLoading();
      if(res.code==1){
        wx.showToast({
          'title':res.msg,
          'icon':'success',
          success:function(){
            wx.removeStorage({
              key: 'label',
              success (res) {
              }
            })
            setTimeout(function(){
              wx.navigateTo({
                url:"/pages/localproject/detail?id="+res.data.id
              })
            },1000)           
          }
        })
       
      }else{
        wx.showToast({
          'title':res.msg,
          'icon':'none'
        })
      }
    })
  },
   /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return this.share()
  },
    /**
   * 用户点击右上角分享
   */
  onShareTimeline: function () {
    return this.share()
  },
  share:function(){
    return {
      title:  '投资项目 发上来赚钱；招商项目 发上来推广',
      path: '/pages/release/project?openid=' +wx.getStorageSync('openid1'),
      imageUrl:  app.globalData.shareIndeximg//自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径。支持PNG及JPG。显示图片长宽比是 5:4。
    } 
  },
  more:function(target){
      console.log(target)
      var id=target.currentTarget.id;
      var more='more'+id;
      console.log(this.data[more])
      if(this.data[more]==1){
        this.setData({[more]:0});
      }else{
        this.setData({[more]:1});
      }
  },
    //上传pdf
    ChoosePDF:function(e){
      var that = this;
      var urls=this.data.urls
      core.upload_file(function(res){
        // console.log(res)
        if(res.error==0){
          //urls.push(res.url);
          that.setData({urls:[res.url]})
        }
      });
    },
  address:function(){
   
    var self=this
     wx.getLocation({
        type: 'gcj02',
        success(res) {
            
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              wx.chooseLocation({
                success(res) {  
                 console.log(res)
                 self.setData({
                    lat: res.latitude,lng:res.longitude,addr_name:res.name,address:res.address
                  })
                },
                fail() {
                  //console.log('地址选择取消')
                }
              })
            },
            fail() {
              console.log('拒绝授权')  
            }
          })
        },
        fail(res) {
          console.log(res)  
        }
    });
  },
})
